﻿using CreateDefect.Domain;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Reflection;
using System.Web;

namespace CreateDefect.Models
{
    public class UserDefect : IDefect
    {
        [Required(ErrorMessage="Summary is required")]
        public string Summary { get; set; }
        [Required(ErrorMessage = "Description is required")]
        public string Description { get; set; }
        [Required(ErrorMessage = "DetectedDate is required")]
        [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd MMM yyyy}")]
        public DateTime? DetectedDate { get; set; }
        [Required(ErrorMessage = "Priority is required")]
        public string Priority { get; set; }
        [Required(ErrorMessage = "Severity is required")]
        public string Severity { get; set; }
        [Required(ErrorMessage = "Status is required")]
        public string Status { get; set; }

        public string Id { get; set; }

        /// <summary>
        /// Cast a QCDefect object to a UserDefect
        /// </summary>
        /// <param name="QCDefect">The QCDefect</param>
        /// <returns>New instance of UserDefect</returns>
        public static explicit operator UserDefect(QCDefect QCDefect)
        {
            try
            {
                Type sourceType = QCDefect.GetType();
                Type destinationType = typeof(UserDefect);

                UserDefect defect = new UserDefect();

                // only get the base type properties
                foreach (PropertyInfo sourceProperty in sourceType.BaseType.GetProperties())
                {
                    PropertyInfo destinationProperty = destinationType.GetProperty(sourceProperty.Name);
                    if (destinationProperty != null)
                        destinationProperty.SetValue(defect, sourceProperty.GetValue(QCDefect, null), null);
                }

                return defect;
            }
            catch { return null; }
        }
    }

    public class DefectField
    {
        public string DisplayName { get; set; }
        public string ColumnName { get; set; }
        public string DataType { get; set; }
        public List<string> Fields { get; set; }
    }

    //public abstract class Defect : IDefect
    //{
    //    [Required(ErrorMessage="Summary is required")]
    //    [DataType(DataType.Text)]
    //    [DisplayName("Summary")]
    //    public abstract string Summary { get; set; }
    //    [Required(ErrorMessage = "Description is required")]
    //    [DataType(DataType.Text)]
    //    [DisplayName("Description")]
    //    public abstract string Description { get; set; }
    //    [Required(ErrorMessage = "Detected Date is required")]
    //    [DataType(DataType.DateTime)]
    //    [DisplayName("Detected on Date")]
    //    public abstract DateTime? DetectedDate { get; set; }
    //    [Required(ErrorMessage = "Priority is required")]
    //    [DataType(DataType.Text)]
    //    [DisplayName("Priority")]
    //    public abstract string Priority { get; set; }
    //    [Required(ErrorMessage = "Severity is required")]
    //    [DataType(DataType.Text)]
    //    [DisplayName("Severity")]
    //    public abstract string Severity { get; set; }
    //    [Required(ErrorMessage = "Status is required")]
    //    [DataType(DataType.Text)]
    //    [DisplayName("Status")]
    //    public abstract string Status { get; set; }
    //}

    public interface IDefect
    {
        string Summary { get; set; }
        string Description { get; set; }
        DateTime? DetectedDate { get; set; }
        string Priority { get; set; }
        string Severity { get; set; }
        string Status { get; set; }
    }
}